from vulnerability.database import CVEDatabase
from vulnerability.tracker import CVETracker

import os
import stat
import time
import subprocess

start_time = time.time()

cve_db = CVEDatabase('fixed_commit_link')

cve_db.add_os_version("openEuler-1.0-LTS", version_type="stable", parent="4.19")
cve_db.add_os_version("OLK-5.10", version_type="stable", parent="5.10")
cve_db.add_os_version("OLK-6.6", version_type="stable", parent="6.6")

cve_db.add_os_version("openEuler-20.03-LTS-SP4", version_type="openEuler", parent="openEuler-1.0-LTS")
cve_db.add_os_version("openEuler-22.03-LTS-SP3", version_type="openEuler", parent="OLK-5.10")
cve_db.add_os_version("openEuler-22.03-LTS-SP4", version_type="openEuler", parent="OLK-5.10")
cve_db.add_os_version("openEuler-24.03-LTS", version_type="openEuler", parent="OLK-6.6")
cve_db.add_os_version("openEuler-24.03-LTS-Next", version_type="openEuler", parent="OLK-6.6")
cve_db.add_os_version("openEuler-24.03-LTS-SP1", version_type="openEuler", parent="OLK-6.6")

script_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vulnerability/scripts")
shell_script_path = os.path.join(script_dir, "init.sh")
st = os.stat(shell_script_path)
if not st.st_mode & stat.S_IEXEC:
# 如果脚本不可执行，则添加执行权限
    os.chmod(shell_script_path, st.st_mode | stat.S_IEXEC)
subprocess.run(
        [shell_script_path],
        check=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT,
        text=True
    )

tracker = CVETracker(cve_db)
tracker.check_immediately()

cve_db.close()

end_time = time.time()
estime = end_time - start_time
print("time: ", estime)